package com.ccl.bigevent.service.impl;

import com.ccl.bigevent.mapper.UserMapper;
import com.ccl.bigevent.pojo.Result;
import com.ccl.bigevent.pojo.User;
import com.ccl.bigevent.service.UserService;
import com.ccl.bigevent.utils.JwtUtil;
import com.ccl.bigevent.utils.Md5Util;
import com.ccl.bigevent.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Title: UserServiceImpl
 * @Author ccl
 * @Package IntelliJ IDEA
 * @Date 2024/10/7 15:36
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 注册
     * @param username
     * @param password
     * @return
     */
    @Override
    public Result register(String username, String password) {
        // 查询用户
        User user = findByUserName(username);

        if (user == null) {
            // 没有占用
            // 注册
            insert(username, password);
            return Result.success();
        }else {
            // 占用
            return Result.error("用户名已被占用");
        }
    }

    @Override
    public void insert(String username, String password) {
        // 加密
        String md5String = Md5Util.getMD5String(password);
        // 插入数据库
        userMapper.insert(username, md5String);
    }

    @Override
    public User findByUserName(String username) {
        // 查询数据库
        User user = userMapper.findByUserName(username);
        return user;
    }

    @Override
    public Result<String> login(String username, String password) {

        // 根据用户名查询用户
        User loginUser = findByUserName(username);

        // 判断用户是否存在
        if (loginUser == null) {
            return Result.error("用户名错误");
        }

        // 判断密码是否正确
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())){
            // 登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);

            ValueOperations<String, Object> ops = redisTemplate.opsForValue();
            ops.set(token, token, 1, TimeUnit.HOURS);

            return Result.success(token);
        }

        return Result.error("密码错误");

    }

    @Override
    public Result<User> userInfo(/*String token*/) {

        // 根据用户名查询用户
//        Map<String, Object> claims = JwtUtil.parseToken(token);
        Map<String, Object> claims = ThreadLocalUtil.get();

        String username = (String) claims.get("username");

        User user = findByUserName(username);
        return Result.success(user);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd), id);

    }


}
